38 plotly
Uncomment the following line to install leafmap if needed.
# !pip install leafmap
import leafmap.plotlymap as leafmap
If you run into an error saying "FigureWidget - 'mapbox._derived' Value Error" (source), uncomment the following line and run it.
# leafmap.fix_widget_error()
Create an interactive map using default settings.
m = leafmap.Map()
m
Change default setting when creating a map.
Can be one of string from "open-street-map", "carto-positron", "carto-darkmatter", "stamen-terrain", "stamen-toner" or "stamen-watercolor" .
m = leafmap.Map(center=(40, -100), zoom=3, basemap="stamen-terrain", height=500)
m
Set map center and zoom level.
m = leafmap.Map(basemap="stamen-watercolor")
m.set_center(lat=20, lon=0, zoom=2)
m
Print out available basemaps.
leafmap.plotly_basemaps.keys()
dict_keys(['OpenStreetMap', 'ROADMAP', 'SATELLITE', 'TERRAIN', 'HYBRID', 'BasemapAT.basemap', 'BasemapAT.grau', 'BasemapAT.highdpi', 'BasemapAT.orthofoto', 'BasemapAT.overlay', 'BasemapAT.surface', 'BasemapAT.terrain', 'CartoDB.DarkMatter', 'CartoDB.DarkMatterNoLabels', 'CartoDB.DarkMatterOnlyLabels', 'CartoDB.Positron', 'CartoDB.PositronNoLabels', 'CartoDB.PositronOnlyLabels', 'CartoDB.Voyager', 'CartoDB.VoyagerLabelsUnder', 'CartoDB.VoyagerNoLabels', 'CartoDB.VoyagerOnlyLabels', 'CyclOSM', 'Esri.DeLorme', 'Esri.NatGeoWorldMap', 'Esri.OceanBasemap', 'Esri.WorldGrayCanvas', 'Esri.WorldImagery', 'Esri.WorldPhysical', 'Esri.WorldShadedRelief', 'Esri.WorldStreetMap', 'Esri.WorldTerrain', 'Esri.WorldTopoMap', 'FreeMapSK', 'Gaode.Normal', 'Gaode.Satellite', 'GeoportailFrance.orthos', 'GeoportailFrance.parcels', 'GeoportailFrance.plan', 'HikeBike.HikeBike', 'HikeBike.HillShading', 'Hydda.Base', 'Hydda.Full', 'Hydda.RoadsAndLabels', 'JusticeMap.americanIndian', 'JusticeMap.asian', 'JusticeMap.black', 'JusticeMap.hispanic', 'JusticeMap.income', 'JusticeMap.multi', 'JusticeMap.nonWhite', 'JusticeMap.plurality', 'JusticeMap.white', 'MtbMap', 'NASAGIBS.BlueMarble', 'NASAGIBS.BlueMarble3031', 'NASAGIBS.BlueMarble3413', 'NASAGIBS.ModisAquaBands721CR', 'NASAGIBS.ModisAquaTrueColorCR', 'NASAGIBS.ModisTerraAOD', 'NASAGIBS.ModisTerraBands367CR', 'NASAGIBS.ModisTerraBands721CR', 'NASAGIBS.ModisTerraChlorophyll', 'NASAGIBS.ModisTerraLSTDay', 'NASAGIBS.ModisTerraSnowCover', 'NASAGIBS.ModisTerraTrueColorCR', 'NASAGIBS.ViirsEarthAtNight2012', 'NASAGIBS.ViirsTrueColorCR', 'NLS', 'OPNVKarte', 'OneMapSG.Default', 'OneMapSG.Grey', 'OneMapSG.LandLot', 'OneMapSG.Night', 'OneMapSG.Original', 'OpenAIP', 'OpenFireMap', 'OpenRailwayMap', 'OpenSeaMap', 'OpenSnowMap.pistes', 'OpenStreetMap.BZH', 'OpenStreetMap.BlackAndWhite', 'OpenStreetMap.CH', 'OpenStreetMap.DE', 'OpenStreetMap.France', 'OpenStreetMap.HOT', 'OpenStreetMap.Mapnik', 'OpenTopoMap', 'SafeCast', 'Stadia.AlidadeSmooth', 'Stadia.AlidadeSmoothDark', 'Stadia.OSMBright', 'Stadia.Outdoors', 'Stamen.Terrain', 'Stamen.TerrainBackground', 'Stamen.TerrainLabels', 'Stamen.Toner', 'Stamen.TonerBackground', 'Stamen.TonerHybrid', 'Stamen.TonerLabels', 'Stamen.TonerLines', 'Stamen.TonerLite', 'Stamen.TopOSMFeatures', 'Stamen.TopOSMRelief', 'Stamen.Watercolor', 'Strava.All', 'Strava.Ride', 'Strava.Run', 'Strava.Water', 'Strava.Winter', 'SwissFederalGeoportal.JourneyThroughTime', 'SwissFederalGeoportal.NationalMapColor', 'SwissFederalGeoportal.NationalMapGrey', 'SwissFederalGeoportal.SWISSIMAGE', 'USGS.USImagery', 'USGS.USImageryTopo', 'USGS.USTopo', 'WaymarkedTrails.cycling', 'WaymarkedTrails.hiking', 'WaymarkedTrails.mtb', 'WaymarkedTrails.riding', 'WaymarkedTrails.skating', 'WaymarkedTrails.slopes', 'nlmaps.grijs', 'nlmaps.luchtfoto', 'nlmaps.pastel', 'nlmaps.standaard', 'nlmaps.water'])
Add a basemap.
m = leafmap.Map()
m.add_basemap("OpenTopoMap")
m
Add XYZ tile layer.
m = leafmap.Map()
tile_url = "https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}"
m.add_tile_layer(tile_url, name="Google Satellite", attribution="Google", opacity=1.0)
m
Add a mapbox tile layer. You will need a mapbox token. The map style can be Can be "basic", "streets", "outdoors", "light", "dark", "satellite", or "satellite-streets".
import os
# os.environ["MAPBOX_TOKEN"] = "your-mapbox-token"
m = leafmap.Map()
m.add_mapbox_layer(style="streets")
m
Remove the modebar in the upper-right corner.
m = leafmap.Map(basemap="stamen-toner")
m
m.clear_controls()
Add more buttons to the modebar.
m = leafmap.Map(basemap="carto-positron")
controls = ['drawline', 'drawopenpath', 'drawclosedpath', 'drawcircle', 'drawrect', 'eraseshape']
m.add_controls(controls)
m
Add Cloud Optimized GeoTIFF.
m = leafmap.Map()
url = 'https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif'
m.add_cog_layer(url, name="Fire (pre-event)")
m
Add a STAC item via HTTP URL.
m = leafmap.Map()
url = 'https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json'
m.add_stac_layer(url, bands=['B3', 'B2', 'B1'], name='False color')
m
Add a STAC item from Microsoft Planetary Computer.
collection = "landsat-8-c2-l2"
items = "LC08_L2SP_047027_20201204_02_T1"
m = leafmap.Map()
m.add_stac_layer(collection=collection, items=items, bands=["SR_B7", "SR_B5", "SR_B4"], titiler_endpoint="pc")
m
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [19], in <module> 1 m = leafmap.Map() ----> 2 m.add_stac_layer(collection=collection, items=items, bands=["SR_B7", "SR_B5", "SR_B4"], titiler_endpoint="pc") 3 m File /opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/leafmap/plotlymap.py:439, in Map.add_stac_layer(self, url, collection, items, assets, bands, titiler_endpoint, name, attribution, opacity, **kwargs) 413 def add_stac_layer( 414 self, 415 url=None, (...) 424 **kwargs, 425 ): 426 """Adds a STAC TileLayer to the map. 427 428 Args: (...) 437 opacity (float, optional): The opacity of the layer. Defaults to 1. 438 """ --> 439 tile_url = stac_tile( 440 url, collection, items, assets, bands, titiler_endpoint, **kwargs 441 ) 442 center = stac_center(url, collection, items, titiler_endpoint) 443 self.add_tile_layer(tile_url, name, attribution, opacity) File /opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/leafmap/common.py:1392, in stac_tile(url, collection, items, assets, bands, titiler_endpoint, **kwargs) 1380 if ( 1381 (assets is not None) 1382 and ("asset_expression" not in kwargs) 1383 and ("expression" not in kwargs) 1384 and ("rescale" not in kwargs) 1385 ): 1386 stats = stac_stats( 1387 collection=collection, 1388 items=items, 1389 assets=assets, 1390 titiler_endpoint=titiler_endpoint, 1391 ) -> 1392 percentile_2 = min([s["percentile_2"] for s in stats]) 1393 percentile_98 = max([s["percentile_98"] for s in stats]) 1394 kwargs["rescale"] = f"{percentile_2},{percentile_98}" File /opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/leafmap/common.py:1392, in <listcomp>(.0) 1380 if ( 1381 (assets is not None) 1382 and ("asset_expression" not in kwargs) 1383 and ("expression" not in kwargs) 1384 and ("rescale" not in kwargs) 1385 ): 1386 stats = stac_stats( 1387 collection=collection, 1388 items=items, 1389 assets=assets, 1390 titiler_endpoint=titiler_endpoint, 1391 ) -> 1392 percentile_2 = min([s["percentile_2"] for s in stats]) 1393 percentile_98 = max([s["percentile_98"] for s in stats]) 1394 kwargs["rescale"] = f"{percentile_2},{percentile_98}" TypeError: string indices must be integers
Add a heat map.
url = 'https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv'
m = leafmap.Map(basemap="stamen-terrain")
m.add_heatmap(url, latitude="Latitude", longitude="Longitude", z="Magnitude", name="Earthquake")
m
Add a choropleth map.
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m = leafmap.Map(basemap="stamen-terrain")
m.add_choropleth_map(url, name="Pop", z="POP_EST", colorscale="Viridis")
m